<template>
	<view class="">
		<!-- 搜索 -->
		<view class="u-p-20 bg_FFFFFF">
			<u-search placeholder="输入商品" v-model="params.search" @blur='custom'></u-search>
		</view>
		<view class="bg_FFFFFF u-p-20">
			<view class="u-flex">
				<view class=" u-p-l-50 u-font-26" @click='show=true'>
					<text class='cl_FF4B3CFF'>{{params.distance==''?'选择距离':`${params.distance}km`}}</text>
					<u-icon class="u-m-l-5" name="arrow-down" color='#666666' size='20'></u-icon>
				</view>
				<view class="u-w-250 u-flex u-row-center" v-for="(t, ti) in filterList" :key="ti"
					@click.stop="priceSwitch(t)">
					<view class="u-font-28">{{ t.title }}</view>
					<view class="u-m-l-20" v-if="t.title!='全部'">
						<view class="" style="margin-top: -10rpx;">
							<u-image src='https://www.sqkjkj.vip/wxImg/top.png' width='16rpx' height='24rpx' v-if="t.value==1"></u-image>
							<u-image src='https://www.sqkjkj.vip/wxImg/dowm.png' width='16rpx' height='24rpx' v-else></u-image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="">
			<view class="u-p-20 u-flex u-row-between">
				<view class="">常用搜索</view>
				<!-- <u-image src="https://www.sqkjkj.vip/wxImg/common/home/clear.png" mode="aspectFill" width="119rpx" height='52rpx'
					@click='clears'></u-image> -->
			</view>
			<view class="u-p-l-20 u-p-r-20 u-flex u-flex-wrap">
				<view class="bg_EFEFEFFF u-text-center u-p-10 u-m-r-20 u-m-b-20 u-font-28" v-for="(item,index) in searchList" :key='index'>
					<view @click='selectLabel(item.name)' class="">
						{{item.name}}
					</view>
				</view>
			</view>
		</view>
		<view class="">
			<view class="u-p-20 u-flex u-row-between">
				<view class="">历史搜索</view>
				<u-image src="https://www.sqkjkj.vip/wxImg/common/home/clear.png" mode="aspectFill" width="119rpx" height='52rpx'
					@click='clearHis'></u-image>
			</view>
			<view class="u-flex u-flex-wrap u-p-l-20 u-p-r-20">
				<view class="bg_EFEFEFFF u-text-center u-p-10 u-m-r-20 u-m-b-20 u-font-28" v-for="(item,index) in historyList" :key='index'>
					<view @click='selectLabel(item)' class="">
						{{item}}
					</view>
				</view>
			</view>
		</view>
		<!-- 商品信息 -->
		<view class="u-p-20 u-flex u-flex-wrap">
			<block  v-for="(item,index) in list">
				<view class="u-w-335 bg_FFFFFF u-m-l-10 u-m-r-10 u-m-b-20 u-b-r-16" v-if="!item.is_ad" :key="index" @click="$u.route(`/pages/home/paymassage?num=${item.spu}`)">
					<!-- <image src="" mode="aspectFit"></image> -->
					<u-image :src="item.imgs[0]" mode="aspectFit" width="335rpx" height='335rpx'>
					</u-image>
					<view class="u-p-20">
						<view class="cl_1C1C1CFF u-weight text-cut">{{item.name}}</view>
						<view class="u-p-t-20">
							<view class="u-flex">
								<u-image v-if="item.is_bjg==1" src="https://www.sqkjkj.vip/wxImg/common/home/img10.png" mode="aspectFill" width="68rpx"
									height='30rpx'></u-image>
								<view class="cl_FC3533FF u-font-30 u-weight">￥{{item.price}}</view>
							</view>
							<view class="u-m-t-10 u-text-right cl_848484FF u-font-24">已售{{item.sales}}</view>
						</view>
						<view class="u-flex  u-p-t-20">
							<u-image :src="item.shop_logo" mode="aspectFill" width="44rpx" height='44rpx'>
							</u-image>
							<view class="cl_848484FF u-m-l-10 u-font-20">{{item.shopname}}</view>
						</view>
					</view>
				</view>
			</block>
		
		</view>
		<u-loadmore :status="status" icon-type="circle" margin-bottom="20" />
		<!-- 点击选择公里范围 -->
		<u-select v-model="show" :list="list1" @confirm='confirm' @cancel="show=false"></u-select>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				text: "5km以内",
				// 常用搜索
				searchList:[],
				// 历史搜索
				historyList:[],
				// 筛选数据
				filterList: this.$list.filterList,
				// 商品数据
				params: {
					type:1,
					page: 1,
					page_size: 20,
					distance:'',
					search:'',
					latitude: 34.793365,
					longitude: 113.599734,
					sort: {
						price: 'desc',
						sales: 'desc'
					}, //数组	非必填	-	-	排序(price总价,virtual_sales销量)例如['price','desc']总价倒序
				},
				list: [],
				list1: [{
						value: '1',
						label: '10'
					},
					{
						value: '2',
						label: '20'
					},
					{
						value: '3',
						label: '30'
					}
				],
				status: 'loadmore',

			}
		},
		onLoad() {
			this.clear();
			this.shop_list()
		},
		onShow() {
			if(uni.getStorageSync('historyList')){
				this.historyList=uni.getStorageSync('historyList')
			}
			this.common_search()
		},
		// 下拉到底事件
		onReachBottom() {
			//避免多次触发
			if (this.status == 'loading' || this.status == 'nomore') {
				return;
			}
			this.shop_list();
		},
		methods: {
			selectLabel(item){
				this.params.search=item
				this.clear();
				this.shop_list()
			},
			// 切换筛选
			priceSwitch(t) {
				t.value = t.value == 0 ? 1 : t.value == 1 ? 2 : t.value == 2 ? 0 : 0
				this.params.sort[t.title == '价格' ? 'price' :'sales'] = t.value == 0 ? '' : t
					.value == 1 ?
					'asc' : 'desc';
				this.clear();
				this.shop_list();
			},
			clear() {
				this.list = [];
				this.params.page = 1;
			},
			shop_list(){
				// #ifdef MP-WEIXIN
				this.params.client = '小程序'
				//  #endif
				
				// #ifndef MP-WEIXIN
				this.params.client = 'app'
				// #endif
				this.$api.shop_list(this.params, res => {
					if(res.data.code==1){
						if (res.data.data.data.length < 20) {
							this.status = 'nomore';
						} else {
							this.params.page = this.params.page + 1;
							this.status = 'loadmore';
						}
						this.list = this.list.concat(res.data.data.data);
						uni.stopPullDownRefresh();
					}
				})
			},
			common_search(){
				this.$api.common_search({}, res => {
					if(res.data.code==1){
						this.searchList=res.data.data
					}
				})
			},
			// 输入商品时获取搜索的商品
			custom() {
				if(this.params.search!=''){
					this.historyList.push(this.params.search)
					uni.setStorageSync('historyList',this.historyList)
				}
				this.clear();
				this.shop_list();
			},
			clearHis(){
				this.historyList=[]
				uni.removeStorageSync('historyList')
			},
			// 点击确定
			confirm(e) {
				this.params.distance = e[0].label
			}
		}
	}
</script>

<style scoped>
	page {
		background: #F5F5F5FF !important;
	}
</style>
